package com.oragraph.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.oragraph.data.Project;
import com.oragraph.data.Tab;
import com.oragraph.data.instance.Instance;
import com.oragraph.data.instance.InstanceFactory;
import com.oragraph.data.instance.InstanceTypeManager;
import com.oragraph.data.sourcedata.DummySourceData;
import com.oragraph.trials.Trial1;

public class NewTabDialog extends JDialog implements ActionListener {
	
	private JComboBox _typesBox, _instancesBox;
	private JTextField _nameField;
	
	public NewTabDialog() {
		super(MainFrame.singleton, "Add new tab", true);
		
		JLabel nameLabel=new JLabel("Name:");
		_nameField=new JTextField(30);
		
		_typesBox=new JComboBox(InstanceTypeManager.getInstanceTypes());
		_typesBox.addActionListener(this);
		_instancesBox=new JComboBox();
		
		this.actionPerformed(null);
		
		JButton OKButton=new JButton("OK");
		OKButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	String selectedType=(String)(_typesBox.getSelectedItem());
            	String selectedInstance=(String)(_instancesBox.getSelectedItem());
            	
            	InstanceFactory factory=InstanceTypeManager.getInstanceFactory(selectedType);
            	Instance root=factory.createByID(selectedInstance);
            	
            	Tab tab=new Tab(_nameField.getText(), root);
            	Project.singleton.addTab(tab);
            	
            	TabPanel panel=new TabPanel(tab);
            	MainFrame.singleton._tabPane.addTab(tab.getName(), panel);            	
            	
            	setVisible(false);
            }
        });	
		
		JButton cancelButton=new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	setVisible(false);
            }
        });	
		
		this.getContentPane().setLayout(new FlowLayout());
		this.getContentPane().add(nameLabel);
		this.getContentPane().add(_nameField);
		this.getContentPane().add(_typesBox);
		this.getContentPane().add(_instancesBox);
		this.getContentPane().add(OKButton);
		this.getContentPane().add(cancelButton);
		
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String selectedType=(String)(_typesBox.getSelectedItem());
	
		// TODO
		if (selectedType.equals("SID")) {
			DefaultComboBoxModel dcbm=new DefaultComboBoxModel(DummySourceData.SIDs);
			_instancesBox.setModel(dcbm);
		}		
		else if (selectedType.equals("IP")) {
			DefaultComboBoxModel dcbm=new DefaultComboBoxModel(DummySourceData.IPs);
			_instancesBox.setModel(dcbm);
		}
		else if (selectedType.equals("USER")) {
			DefaultComboBoxModel dcbm=new DefaultComboBoxModel(DummySourceData.users);
			_instancesBox.setModel(dcbm);
		}
		
	}

}
